/*
 * Created by Jaren at 2021/10/29 14:13
 */
import 'dart:typed_data';

import 'package:binary/binary.dart';
import 'package:inmotion_protocol/common/mode.dart';
import 'package:inmotion_protocol/common/vehicle_info.dart';
import 'package:inmotion_protocol/ezcan/bean/real_time_info.dart';
import 'package:inmotion_protocol/util/byte_util.dart';

class L8AllRealTimeInfo extends AbsAllRealTimeInfo {
  L8AllRealTimeInfo(AbsRealTimeInfo realTimeInfo, AbsRealTimeState realTimeState, AbsRealTimeError realTimeError) : super(realTimeInfo, realTimeState, realTimeError);
}

class L8RealTimeInfo extends AbsRealTimeInfo {
  Int8 motorTemp = Int8.zero;
  Int8 batteryTemp = Int8.zero;

  L8RealTimeInfo.fromFastMsgRsp(ProductProperty property, Uint8ClampedList bytes) : super(property) {
    speed = ByteUtil.toInt16(ByteUtil.bytes2IntLe(bytes, 12, 4) ~/ 38.12);
    voltage = ByteUtil.toInt16(ByteUtil.bytes2IntLe(bytes, 24, 4));
    power = ByteUtil.toInt32((current.value * voltage.value * 0.01).toInt());
    mosTemp = ByteUtil.toInt8(bytes[32] > bytes[33] ? bytes[32] : bytes[33]);
    totalMileage = ByteUtil.toUint32(ByteUtil.bytes2Uint32Le(bytes, 44).value * 10);
  }

  @override
  String toString() {
    return 'L8RealTimeInfo{estimatedTotalMileage: ${estimatedTotalMileage.value}, mosTemp: ${mosTemp.value}, '
        'power: ${power.value}, voltage: ${voltage.value}, current: ${current.value}, speed: ${speed.value}, '
        'mileage: ${mileage.value}, batteryLevel: ${batteryLevel.value}, motorTemp: $motorTemp, batteryTemp: $batteryTemp}';
  }
}

class L8RealTimeState extends AbsRealTimeState {
  L8RealTimeState.fromFastMsgRsp(Uint8ClampedList bytes) : super.fromBytes(bytes) {
    pcModeValue = ByteUtil.bytes2Uint32Le(bytes, 60);
    if (pcModeValue.value & 0xF0 != 0) {
      pcMode = Mode.idle;
      lockState = Uint4(1);
    } else {
      pcMode = Mode.working;
      lockState = Uint4(0);
    }
  }

  L8RealTimeState.fromSlowMsgRsp(Uint8ClampedList bytes) : super.fromBytes(bytes) {
    headlightState = ByteUtil.bytes2Uint4Le(bytes, 80);
  }
}

class L8RealTimeError extends AbsRealTimeError {
  Uint4 overCurrentState = Uint4.zero; // 过流
  Uint4 lowVoltageState = Uint4.zero; // 低压
  Uint4 overBoardTempState = Uint4.zero; // 驱动板温度过高
  Uint4 overPhaseCurrentState = Uint4.zero; // 相电流过大
  Uint4 phaseCurrentState = Uint4.zero; // 相电流错误
  Uint4 motorHallState = Uint4.zero; // 电机霍尔异常
  Uint4 comState = Uint4.zero; // 通讯错误
  Uint4 phaseCurrentInitState = Uint4.zero; // 相电流零点错误
  Uint4 voltageState = Uint4.zero; // 电压错误

  L8RealTimeError.fromErrorCodeMsgRsp(Uint8ClampedList bytes) : super.fromBytes(bytes) {
    originalValue = ByteUtil.bytes2Uint32Le(bytes);
    int v = originalValue.value;
    // errors
    if ((v & 0x01) == 0x01) {
      overCurrentState = Uint4(1);
    }
    if ((v & 0x02) == 0x02) {
      lowVoltageState = Uint4(1);
    }
    if ((v & 0x04) == 0x04) {
      overBoardTempState = Uint4(1);
    }
    if ((v & 0x08) == 0x08) {
      overPhaseCurrentState = Uint4(1);
    }
    if ((v & 0x10) == 0x10) {
      phaseCurrentState = Uint4(1);
    }
    if ((v & 0x20) == 0x20) {
      motorHallState = Uint4(1);
    }
    if ((v & 0x40) == 0x40) {
      comState = Uint4(1);
    }
    if ((v & 0x80) == 0x80) {
      phaseCurrentInitState = Uint4(1);
    }
    if ((v & 0xF0) == 0xF0) {
      voltageState = Uint4(1);
    }
  }
}
